﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Photography.App_Data;

namespace Photography.Admin
{
    public partial class AddGallery : System.Web.UI.Page
    {
        protected new Photography.Admin.AdminMaster Master { get { return base.Master as Photography.Admin.AdminMaster; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Master.AdminID.HasValue)
                Response.Redirect("Default.aspx");

            if (!IsPostBack)
                if (Master.GalleryId.HasValue)
                {
                    btnSave.Text = "Save Gallery";

                    Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
                    GetGalleryInfoResult gallery = context.GetGalleryInfo(Master.GalleryId).FirstOrDefault();
                    txtName.Text = gallery.Name;
                    txtDescription.Text = gallery.Description;
                    cboCategory.SelectedValue = gallery.CategoryID.ToString();
                    cboCategory.Visible = false;
                    lblCategory.Text = cboCategory.Text;
                }
                else
                {
                    cboCategory.SelectedValue = Master.CategoryId.ToString();
                }
        }

        protected void GetCategories(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetCategories();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();

            if (!Master.GalleryId.HasValue)
                context.AddGallery(txtName.Text, int.Parse(cboCategory.SelectedValue), null, txtDescription.Text, null);
            else
                context.UpdateGallery(Master.GalleryId, txtName.Text, null, null, txtDescription.Text, null);

            Master.RedirectToManageGalleries();
        }

    }
}
